package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.DamageListDao;
import com.atguigu.jxc.dao.DamageListGoodsDao;
import com.atguigu.jxc.entity.DamageList;
import com.atguigu.jxc.entity.DamageListGoods;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.service.DamageService;
import com.atguigu.jxc.util.DateUtil;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

@Service
public class DamageServiceImpl implements DamageService {

    @Autowired
    private DamageListDao damageListDao;
    @Autowired
    private DamageListGoodsDao damageListGoodsDao;

    /**
     * 保存报损单
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(String damageNumber, String damageDate, String remarks, String damageListGoodsStr,Integer userId) {
        DamageList damageList = new DamageList();
        damageList.setDamageNumber(damageNumber);
        damageList.setDamageDate(damageDate);
        damageList.setRemarks(remarks);
        damageList.setUserId(userId);
        damageListDao.saveDamageList(damageList);
        //damageListGoodsStr转换
        Gson gson = new Gson();
        DamageListGoods[] goodsArray = gson.fromJson(damageListGoodsStr, DamageListGoods[].class);
        for (DamageListGoods goods : goodsArray) {
            damageListGoodsDao.saveDamageListGoods(damageList.getDamageListId(),goods);
        }



    }

    /**
     * 报损单查询
     */
    @Override
    public List<DamageList> list(String sTime, String eTime) {
        return damageListDao.selectDamageList(sTime,eTime);
    }
}
